﻿// This software is the confidential and proprietary information of NetBay.
// Such confidential information should not be disclosed.
// NetBay copyright(c) 2011

#region

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using NetBay.Core.BusinessEntities.Workflow;
using Rio.Framework.Localisation;
using Rio.Framework.Pages.Controls.Models;
using Rio.Framework.Pages.Manager;
using Rio.Framework.Pages.Workflow.Actions;

#endregion

namespace Rio.Framework.Pages.Workflow
{
    public partial class WorkflowToolBar : INotifyPropertyChanged, IDisposable
    {
        #region Readonly & Static Fields

        public static readonly DependencyProperty WorkflowManagerProperty =
            DependencyProperty.Register("WorkflowManager",
                                        typeof (WorkflowManager),
                                        typeof (WorkflowToolBar),
                                        new PropertyMetadata(OnWorkflowManagerLoaded));

        #endregion

        #region Fields

        private ObservableCollection<WorkflowButtonAction> _availableActions;

        private ObservableCollection<ISelectableItem<WorkflowState>> _filters;
        private ISelectableItem<WorkflowState> _selectedFilter;

        #endregion

        #region Constructors

        public WorkflowToolBar()
        {
            InitializeComponent();

            InitLabels();
        }

        #endregion

        #region Instance Properties

        public ObservableCollection<WorkflowButtonAction> AvailableActions
        {
            get
            {
                return _availableActions;
            }
            set
            {
                _availableActions = value;
                OnPropertyChanged("AvailableActions");
            }
        }

        public ObservableCollection<ISelectableItem<WorkflowState>> Filters
        {
            get
            {
                return _filters;
            }
            set
            {
                _filters = value;

                OnPropertyChanged("Filters");
            }
        }

        public ISelectableItem<WorkflowState> SelectedFilter
        {
            get
            {
                return _selectedFilter;
            }
            set
            {
                _selectedFilter = value;
                OnPropertyChanged("SelectedFilter");

                if (SelectedFilter != null)
                    WorkflowManager.SelectedFilter = SelectedFilter.Value;
            }
        }

        public WorkflowManager WorkflowManager
        {
            get
            {
                return (WorkflowManager) GetValue(WorkflowManagerProperty);
            }
            set
            {
                SetValue(WorkflowManagerProperty, value);
            }
        }

        #endregion

        #region Instance Methods

        /// <summary>
        ///   Activates the rules.
        /// </summary>
        public void LoadActions()
        {
            //Load Actions
            AvailableActions = new ObservableCollection<WorkflowButtonAction>(WorkflowManager.GetAvailableActions().OrderBy(a => a.IsGlobalAction));
        }

        /// <summary>
        ///   Loads the filters.
        /// </summary>
        public void LoadFilters()
        {
            if (WorkflowManager.Filters != null)
            {
                Filters = new ObservableCollection<ISelectableItem<WorkflowState>>(WorkflowManager.Filters);
                _selectedFilter =
                    Filters.Where(
                        i => WorkflowManager.SelectedFilter != null && i.Value == WorkflowManager.SelectedFilter.Value).
                        SingleOrDefault();
                OnPropertyChanged("SelectedFilter");
            }
        }

        /// <summary>
        ///   Called when [property changed].
        /// </summary>
        /// <param name="propertyName"> Name of the property. </param>
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        ///   Inits the labels.
        /// </summary>
        private void InitLabels()
        {
            filterByLabel.Text = Resx.GetLabel(ResxWorkflow.Workflow_FilterBy);
        }

        private void InitializeWorkflow()
        {
            if (WorkflowManager != null)
            {
                WorkflowManager.ActionsChanged += WorkflowManagerActionsChanged;
            }
        }

        #endregion

        #region Event Handling

        private void WorkflowManagerActionsChanged(object sender, EventArgs e)
        {
            LoadActions();
            LoadFilters();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (WorkflowManager != null)
            {
                WorkflowManager.ActionsChanged -= WorkflowManagerActionsChanged;
            }

            if (AvailableActions != null)
            {
                foreach (var workflowButtonAction in AvailableActions)
                    workflowButtonAction.Dispose();

                AvailableActions.Clear();
            }

            if (Filters != null)
                Filters.Clear();
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Class Methods

        public static void OnWorkflowManagerLoaded(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            WorkflowToolBar workflowToolBar = source as WorkflowToolBar;

            if (workflowToolBar != null)
            {
                workflowToolBar.InitializeWorkflow();
            }
        }

        #endregion
    }
}